package CCNQ::Proxy::report;
# Copyright (C) 2006, 2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#
use strict; use warnings;

use base qw(CCNQ::Proxy::Base);

sub list_of_timestamps
{
    opendir(my $dh, '/var/log/billing') or die "Can't open /var/log/billing";
    my @timestamps = sort { $b cmp $a } grep { /^\d{8}-\d{4}$/ } readdir($dh);
    close($dh);
    return @timestamps;
}

sub insert
{
}

#
#   _do_command_table($command,@command_args)
#

sub _do_command_table
{
    our $self = shift;
    my $command = shift;
    my $command_args = join(' ', map { qq("$_") } @_);

    my @params = $self->vars;
    our %params = @params;


    my $server      = $params{server};
    die 'No server' unless defined $server;
    die 'No such server' unless grep { $_ eq $server } ($self->list_of_servers);

    my $ssh_server = $server;

    my $subscriber  = $params{subscriber};
    $subscriber = '' if not defined $subscriber;
    die 'Invalid subscriber name' unless $subscriber =~ /^[\w-]*$/;

    my $timestamp      = $params{timestamp};
    my $files;
    # Realtime
    if(not defined $timestamp or $timestamp eq '')
    {
        $timestamp = '';
        $files = '/var/log/openser/acc_*.log';
    }
    # Non-realtime
    else
    {
        $ssh_server = 'localhost';
        $files = "/var/log/billing/$timestamp/server-$server.log.bz2";
    }

    # XXX This assumes all servers in the cluster have the same "install_dir".
    my $shell_command =
        qq(${configuration::install_dir}/CCNQ/Proxy/bin/${command} ${command_args} "${server}" "${timestamp}" "${subscriber}");

    if( ${files} =~ /bz2$/ )
    {
        $shell_command = "bzcat ${files} | ${shell_command}";
    }
    else
    {
        $shell_command = "${shell_command} ${files}";
    }

    warn($shell_command);

    print qq(<table class="ccn_report">\n);

    if( ${ssh_server} eq 'localhost' or ${ssh_server} =~ /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ )
    {
        open(DATA,qq(${shell_command} |))
            or die "start: $!";
    }
    else
    {
        my ($ssh_server_name,$ssh_server_port) = split(/:/,$ssh_server);
        $ssh_server_port = 22 if not defined $ssh_server_port;
        open(DATA,qq(ssh -p '${ssh_server_port}' '${ssh_server_name}' '${shell_command}' |))
            or die "start: $!";        
    }
    while(<DATA>)
    {
        chomp;
        my @fields = split(/\t/);
        print '<tr>';
        if($fields[0] =~ /^\s+$/)
        {
            print join('', map { qq(<th class="ccn_report">$_</th>)} @fields );
        }
        else
        {
            print join('', map { s{\(([^)]+)\)}{<br><small>$1</small>}; qq(<td align="char" char="." class="ccn_report">$_</td>)} @fields );
        }
        print "</tr>\n";
    }
    close(DATA);
    print "</table>\n";
}

1;
